package com.zhouxiaoxuan13.consumer.feign.fallback;

import com.zhouxiaoxuan13.consumer.feign.LoadBalancerServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 负载均衡测试服务Feign客户端降级处理类
 * 当负载均衡测试服务不可用时提供默认响应
 */
@Slf4j
@Component
public class LoadBalancerServiceFallback implements LoadBalancerServiceClient {

    @Override
    public ResponseEntity<Map<String, Object>> getCurrentInstance() {
        log.warn("负载均衡测试服务不可用，返回降级响应 - getCurrentInstance");
        Map<String, Object> fallbackResponse = createFallbackResponse();
        fallbackResponse.put("message", "负载均衡测试服务暂时不可用");
        return ResponseEntity.ok(fallbackResponse);
    }

    @Override
    public ResponseEntity<Map<String, Object>> getInfo() {
        log.warn("负载均衡测试服务不可用，返回降级响应 - getInfo");
        Map<String, Object> fallbackResponse = createFallbackResponse();
        fallbackResponse.put("requestNumber", -1);
        return ResponseEntity.ok(fallbackResponse);
    }

    @Override
    public ResponseEntity<Map<String, Object>> getStats() {
        log.warn("负载均衡测试服务不可用，返回降级响应 - getStats");
        Map<String, Object> fallbackResponse = createFallbackResponse();
        fallbackResponse.put("totalRequests", -1);
        fallbackResponse.put("loadBalancerType", "Fallback Response");
        return ResponseEntity.ok(fallbackResponse);
    }

    @Override
    public ResponseEntity<Map<String, Object>> health() {
        log.warn("负载均衡测试服务不可用，返回降级响应 - health");
        Map<String, Object> fallbackResponse = new HashMap<>();
        fallbackResponse.put("status", "DOWN");
        fallbackResponse.put("port", "N/A");
        fallbackResponse.put("service", "负载均衡测试服务");
        fallbackResponse.put("timestamp", LocalDateTime.now().toString());
        fallbackResponse.put("message", "服务暂时不可用");
        return ResponseEntity.ok(fallbackResponse);
    }

    @Override
    public ResponseEntity<Map<String, Object>> resetCounter() {
        log.warn("负载均衡测试服务不可用，无法重置计数器 - resetCounter");
        Map<String, Object> fallbackResponse = new HashMap<>();
        fallbackResponse.put("message", "服务不可用，无法重置计数器");
        fallbackResponse.put("timestamp", LocalDateTime.now().toString());
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(fallbackResponse);
    }

    // ==================== 熔断器测试接口降级处理 ====================

    @Override
    public ResponseEntity<Map<String, Object>> slowResponse(int delay) {
        log.warn("负载均衡测试服务不可用，返回降级响应 - slowResponse，延迟: {}ms", delay);
        Map<String, Object> fallbackResponse = createFallbackResponse();
        fallbackResponse.put("message", "慢响应测试服务暂时不可用");
        fallbackResponse.put("requestedDelay", delay + "ms");
        fallbackResponse.put("actualDelay", "0ms (fallback)");
        return ResponseEntity.ok(fallbackResponse);
    }

    @Override
    public ResponseEntity<Map<String, Object>> errorResponse(int errorRate) {
        log.warn("负载均衡测试服务不可用，返回降级响应 - errorResponse，错误率: {}%", errorRate);
        Map<String, Object> fallbackResponse = createFallbackResponse();
        fallbackResponse.put("message", "异常测试服务暂时不可用");
        fallbackResponse.put("requestedErrorRate", errorRate + "%");
        fallbackResponse.put("actualResult", "fallback response");
        return ResponseEntity.ok(fallbackResponse);
    }

    @Override
    public ResponseEntity<Map<String, Object>> unavailableResponse() {
        log.warn("负载均衡测试服务不可用，返回降级响应 - unavailableResponse");
        Map<String, Object> fallbackResponse = createFallbackResponse();
        fallbackResponse.put("message", "服务不可用测试功能暂时不可用");
        fallbackResponse.put("originalStatus", "503 (Service Unavailable)");
        fallbackResponse.put("fallbackStatus", "200 (Fallback Response)");
        return ResponseEntity.ok(fallbackResponse);
    }

    /**
     * 创建通用降级响应
     */
    private Map<String, Object> createFallbackResponse() {
        Map<String, Object> response = new HashMap<>();
        response.put("serviceName", "负载均衡测试服务");
        response.put("port", "N/A");
        response.put("instanceId", "fallback-instance");
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("status", "fallback");
        return response;
    }
}
